package org.vectrics.common.util;

import org.apache.log4j.Category;

public class PeriodicTask extends Thread {
    private static Category log = Category.getInstance(PeriodicTask.class);
    private volatile boolean stopped = false;
    private volatile boolean fIgnoreMultiple = true;

    public PeriodicTask() {
    }

    public void setIgnoreMulitple(boolean value) {
        fIgnoreMultiple = value;
    }

    public void run() {
        int runCount = 0;
        boolean fRunning = false;
        
        log.info("PeriodicTask Starting.  Task Class = " + task.getClass().getName());
        while ((!stopped) && ((numberOfRuns < 0) || (runCount < numberOfRuns))) {
            try {
                if (task == null) {
                    log.error("No task assigned to PeriodicTask element.  Nothing performed.");
                }
                else if (fRunning == false) {
                    fRunning = true;
                    try {
                        log.info("PeriodicTask running:  Task Class = " + task.getClass().getName());
                        task.run();
                    }
                    catch (Exception ex) {
                        log.error("run() - error running task.", ex);
                    }
                    
                    fRunning = false;
                }
                else {
                    /*
                        if (fIgnoreMultiple == false) { 
                            try {
                                log.info("PeriodicTask running:  Task Class = " + task.getClass().getName());
                                task.run();
                            }
                            catch (Exception ex) {
                                log.error("run() - error running task", ex);
                            }
                        }
                      */  
                        log.info("run() - perioidic task attempted to run, but was already running.");
                }

                runCount++;
                
                Thread.sleep(suspendSeconds * 1000);
                log.info("PeriodicTask finished with suspend.  Task Class = " + task.getClass().getName());
            }
            catch (java.lang.InterruptedException ie) {
                log.error("Sleep of PeriodicTask was interupted", ie);
            }
            
        }
        log.info("PeriodicTask completed.  Task Class = " + task.getClass().getName());
    }

    public synchronized void terminate() {
        stopped = true;

    }

    public void setSuspendSeconds(long suspendSeconds) {
        this.suspendSeconds = suspendSeconds;
    }
    public long getSuspendSeconds() {
        return suspendSeconds;
    }
    public void setTask(PeriodicRunnable task) {
        this.task = task;
    }
    public PeriodicRunnable getTask() {
        return task;
    }
    public void setNumberOfRuns(int numberOfRuns) {
        this.numberOfRuns = numberOfRuns;
    }
    public int getNumberOfRuns() {
        return numberOfRuns;
    }
    private long suspendSeconds = 60 * 5;  // 5 minutes
    private PeriodicRunnable task = null;
    private int numberOfRuns = -1;
}